﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace Acoris.Markdown
{
    /// <summary>
    /// 表示AML文档的内容之一
    /// </summary>
    internal sealed class AMLCollection: AMLParseNode, IEnumerable<AMLParseNode>
    {
        List<AMLParseNode> m_collection;

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="offset"></param>
        /// <param name="length"></param>
        /// <param name="document"></param>
        /// <param name="nameOffset"></param>
        /// <param name="nameLength"></param>
        /// <param name="textOffset"></param>
        /// <param name="textLength"></param>
        public AMLCollection(
            AMLParseNode parent,
            int offset,
            int length,
            string document,
            int nameOffset,
            int nameLength,
            int textOffset,
            int textLength)
            :base(parent, AMLNodeType.Collection, offset, length, document, nameOffset, nameLength, textOffset, textLength)
        {
            m_collection = new List<AMLParseNode>();
        }

        /// <summary>
        /// 添加一个AMLItem到集合之中
        /// </summary>
        /// <param name="item"></param>
        public void Add(AMLItem item)
        {
            Add(item);
        }

        /// <summary>
        /// 添加一个AMLCollection到集合之中
        /// </summary>
        /// <param name="collection"></param>
        public void Add(AMLCollection collection)
        {
            Add(collection);
        }

        /// <summary>
        /// 添加一个AML节点到集合之中
        /// </summary>
        /// <param name="node"></param>
        private void Add(AMLParseNode node)
        {
            m_collection.Add(node);
        }

        /// <summary>
        /// 移除指定的节点
        /// </summary>
        /// <param name="node"></param>
        public void Remove(AMLParseNode node)
        {
            m_collection.Remove(node);
        }

        /// <summary>
        /// 返回用与当前集合遍历的枚举数
        /// </summary>
        /// <returns>返回用与当前集合遍历的枚举数</returns>
        IEnumerator<AMLParseNode> IEnumerable<AMLParseNode>.GetEnumerator()
        {
            return m_collection.GetEnumerator();
        }

        /// <summary>
        /// 返回用与当前集合遍历的枚举数
        /// </summary>
        /// <returns>返回用与当前集合遍历的枚举数</returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return m_collection.GetEnumerator();
        }

        internal int TextOffset {
            get { return m_textOffset; }
            set { m_textOffset = value; }
        }

        internal int TextLength {
            get { return m_textLength; }
            set { m_textLength = value; }
        }

        /// <summary>
        /// 返回与当前实例等价的字符串形式
        /// </summary>
        /// <returns>返回与当前实例等价的字符串形式</returns>
        public new string ToString()
        {
            return $"<{TagName},count:{m_collection.Count}>";
        }
    }
}
